package be.phl.obstacleapp;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import be.phl.webservice.User;

public class MainData {

	private static User LoggedUser;
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss", Locale.FRANCE);
	private static final String SERVER_URL = "http://193.190.154.248/ObstacleWS/webresources/Obstacle";
	private	static int SCREENING_ROOM_DETAIL_VIEW_SELECTED = 2;
	
	/**
	 * get the user data from the logged on user
	 * 
	 * @return
	 */
	public static User getLoggedUser() {
		return LoggedUser;
	}

	/**
	 * set the logged user
	 * 
	 * @param loggedUser
	 */
	public static void setLoggedUser(User loggedUser) {
		LoggedUser = loggedUser;
	}

	public static SimpleDateFormat getDateformat() {
		return dateFormat;
	}

	/**
	 * Get the url from the webservice
	 * 
	 * @return
	 */
	public static String getServerUrl() {
		return SERVER_URL;
	}

	/**
	 * Check if Internet is available
	 * 
	 * @return
	 */
	public static boolean isNetworkAvailable(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		// if no network is available networkInfo will be null
		// otherwise check if we are connected
		if (networkInfo != null && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}

	/**
	 * get the selected layout for the screeningproblems
	 * @return
	 */
	public static int getSCREENING_ROOM_DETAIL_VIEW_SELECTED() {
		return SCREENING_ROOM_DETAIL_VIEW_SELECTED;
	}

	public static void setSCREENING_ROOM_DETAIL_VIEW_SELECTED(
			int sCREENING_ROOM_DETAIL_VIEW_SELECTED) {
		SCREENING_ROOM_DETAIL_VIEW_SELECTED = sCREENING_ROOM_DETAIL_VIEW_SELECTED;
	}

	public static SSLContext getSSLContext() {
		String keystoreType = "JKS";
		InputStream keystoreLocation = null;
		char[] keystorePassword = null;
		char[] keyPassword = null;

		KeyStore keystore;
		try {
			keystore = KeyStore.getInstance(keystoreType);
			keystore.load(keystoreLocation, keystorePassword);
			KeyManagerFactory kmfactory = KeyManagerFactory
					.getInstance(KeyManagerFactory.getDefaultAlgorithm());
			kmfactory.init(keystore, keyPassword);

			InputStream truststoreLocation = null;
			char[] truststorePassword = null;
			String truststoreType = "JKS";

			KeyStore truststore = KeyStore.getInstance(truststoreType);
			truststore.load(truststoreLocation, truststorePassword);
			TrustManagerFactory tmfactory = TrustManagerFactory
					.getInstance(TrustManagerFactory.getDefaultAlgorithm());

			KeyManager[] keymanagers = kmfactory.getKeyManagers();
			TrustManager[] trustmanagers = tmfactory.getTrustManagers();
			
			SSLContext sslContext = SSLContext.getInstance("TLS");
			sslContext.init(keymanagers, trustmanagers, new SecureRandom());
			SSLContext.setDefault(sslContext);

			return sslContext;
			
		} catch (KeyStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CertificateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnrecoverableKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (KeyManagementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
}
